/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.lib;

import com.bff.mpd.utils.MPDComparator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.objects.MPDAlbum;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author bill
 */
public class MusicObjects {

    private static List<MPDAlbum> albums;
    private static List<MPDArtist> artists;
    private static List<String> years;

    private static MPDComparator comparator = new MPDComparator();

    //private static HashMap<String, SlimAlbum> albumMap;
    // private static HashMap<String, SlimArtist> artistMap;
    public static Collection<MPDAlbum> getAlbums() {
        if (albums == null) {
            refreshAlbums();
        }

        Collections.sort(albums, comparator);

        return albums;
    }

    public static Collection<MPDArtist> getArtists() {
        if (artists == null) {
            refreshArtists();
        }

        Collections.sort(artists, comparator);

        return artists;
    }

    public static Collection<String> getYears() {
        if (years == null) {
            refreshYears();
        }

        Collections.sort(years);

        return years;
    }

    public static void refreshAlbums() {
        try {
            albums = new ArrayList<MPDAlbum>(JukeboxProperties.getInstance().getDatabase().listAllAlbums());
            Collections.sort(albums, comparator);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void refreshArtists() {
        try {
            artists = new ArrayList<MPDArtist>(JukeboxProperties.getInstance().getDatabase().listAllArtists());
            Collections.sort(artists, comparator);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void refreshYears() {
        try {
            years = new ArrayList<String>(JukeboxProperties.getInstance().getDatabase().listAllYears());
            years.remove("0");
            years.remove(" ");
            years.remove("");
            Collections.sort(years);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(MusicObjects.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void refreshAll() {
        refreshAlbums();
        refreshArtists();
        refreshYears();
    }
}
